package com.yaorange.web;

import com.github.pagehelper.PageInfo;
import com.yaorange.entity.Customer;
import com.yaorange.entity.CustomerSource;
import com.yaorange.entity.Dept;
import com.yaorange.entity.Emp;
import com.yaorange.service.CustomerService;
import com.yaorange.service.CustomerSourceService;
import com.yaorange.service.DeptService;
import com.yaorange.service.EmpService;
import com.yaorange.vo.AllocateVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

@RestController
@CrossOrigin
@RequestMapping("/market/allocation")
public class MarketAllocatedController {

    @Resource
    private EmpService empService;

    @Resource
    private DeptService deptService;

    @Resource
    private CustomerService customerService;

    @Resource
    private CustomerSourceService customerSourceService;


    //获取部门列表，用于下拉框
    @GetMapping("/deptList")
    public List<Dept> getBusinessDept(){
        //只获取业务一部和业务二部（模糊查询）
        return deptService.getBusinessDeptList();
    }


    //获取部门下的在职员工(与部门列表级联)
    @GetMapping("empList/{deptId}")
    public List<Emp> empListByDeptId(@PathVariable Integer deptId){
        return empService.getEmpListByDeptId(deptId);
    }


    //获取所有"客户来源"数据
    @GetMapping("/sources")
    public List<CustomerSource> getSourceList(){
        return customerSourceService.getList();
    }


    //有指定的account添加员工（知道是那个emp添加的客户）
    @PostMapping("/{account}")
    public Map<String,Object> add(@PathVariable String account, @RequestBody Customer customer){
        Map<String, Object> map = new HashMap<>();
        Emp emp = empService.getEmpByUserName(account);
        if(emp == null){
            map.put("msg","账号异常，请重新登录");
            return map;
        }
        //设置跟踪字段数据
        //TODO:设置操作员工
        //设置客户等级
        customer.setCustomerLevel("优");
        //设置关注等级
        customer.setAttentionLevel("A");
        //设置客户可见性(避免自动释放到公共池)
        customer.setVisibility(0);
        //设置客户录入客户类型
        customer.setAddType(0);
        //设置录入人员id
        customer.setAddPersonId(emp.getEmpId());
        //设置录入人员名称
        customer.setAddPersonName(emp.getEmpName());


        //进行添加到数据库的操作
        int rows=customerService.add(customer);
        if(rows > 0){
            map.put("status",20000);
            map.put("msg","新增客户成功");
        }else {
            map.put("msg","新增客户失败");
        }
        return map;
    }


    //新增客户列表分页数据
    @GetMapping("/page/{pageNum}/{pageSize}/{account}")
    public PageInfo<Customer> customerList(@PathVariable Integer pageNum,
                                           @PathVariable Integer pageSize, @PathVariable String account){
        Emp emp = empService.getEmpByUserName(account);
        if(emp == null){
            return null;
        }
        return customerService.customerList(pageNum,pageSize,emp.getEmpId());
    }


    //新增客户列表的分配功能实现
    @PutMapping
    public String update(@RequestBody Customer customer){
        //设置客户状态为：分配到员工
        customer.setState(Customer.CustomerState.allocatedToEmp.ordinal());
        int rows = customerService.update(customer);
        if(rows > 0){
            return "ok";
        }
        return "error";
    }


    //导入客户名单列表的分页数据
    @GetMapping("/customerRosterList/{account}")
    public List<AllocateVo> customerRosterList(@PathVariable String account){
        //通过账户获取员工对象
        Emp emp = empService.getEmpByUserName(account);
        if(emp == null){
            return null;
        }
        List<Map<String, Object>> rosterList = customerService.getRosterList(emp.getEmpId());
        if(rosterList.size() == 0){
            return null;
        }
        //解析Map组装VO
        List<AllocateVo> allocateVoList = new ArrayList<>();
        for (Map<String, Object> map : rosterList) {
            AllocateVo allocateVo = new AllocateVo();
            allocateVo.setName((String)map.get("name"));
            allocateVo.setDate((Date)map.get("date"));
            allocateVo.setNumber((Long) map.get("number"));
            allocateVo.setAllocated((Long) map.get("allocated"));

            allocateVoList.add(allocateVo);
        }
        return allocateVoList;
    }


    //
    @GetMapping("/roster/page/{pageNum}/{pageSize}/{rosterName}")
    public PageInfo<Customer> getRosterPage(@PathVariable Integer pageNum,@PathVariable Integer pageSize,@PathVariable String rosterName){
        return customerService.getRosterPage(pageNum,pageSize,rosterName);
    }


    //
    @PutMapping("/{deptId}")
    public String updateCustomer(@PathVariable Integer deptId,@RequestBody List<Integer> customerIds){
        if(customerIds.size() > 0){
            int rows = customerService.updateCustomerByIds(customerIds,deptId);
            if(rows > 0){
                return "ok";
            }
        }
        return "error";
    }



    @PutMapping("/{deptId}/{rosterName}")
    public String updateCustomer(@PathVariable Integer deptId,@PathVariable String rosterName){
        int rows = customerService.updateCustomerByRosterName(rosterName,deptId);
        if(rows > 0){
            return "ok";
        }
        return "error";
    }
}
